//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using System.Security.Permissions;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace EntLibContrib.Resource.Configuration.Design.Editors
{
  /// <summary>
  /// <para>Provides a user interface for selecting an <see cref="AssemblyResourceName"/>.</para>
  /// </summary>
  /// <remarks>
  /// <para><see cref="ResourceSelectorEditor"/> is a <see cref="UITypeEditor"/> that provides a dialog box for selecting an <see cref="AssemblyResourceName"/>.</para>
  /// </remarks>
  [PermissionSet(SecurityAction.InheritanceDemand, Name="FullTrust")]
  [PermissionSet(SecurityAction.LinkDemand, Name="FullTrust")]
  public class ResourceSelectorEditor : UITypeEditor
  {
		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="ResourceSelectorEditor"/> class.
		/// </summary>
    public ResourceSelectorEditor() : base()
    {
    }
		#endregion

		#region Public Methods
		/// <summary>
		/// Edits the specified object's value using the editor style indicated by <seealso cref="UITypeEditor.GetEditStyle()"/>.
		/// </summary>
		/// <param name="context">An <see cref="ITypeDescriptorContext"/> that can be used to gain additional context information.</param>
		/// <param name="provider">An <see cref="IServiceProvider"/> that this editor can use to obtain services.</param>
		/// <param name="value">The object to edit.</param>
		/// <returns>
		/// A selected <see cref="AssemblyResourceName"/> object instance.
		/// </returns>
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
      if (provider != null)
      {
        IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
        if (editorService != null)
        {
          AssemblyResourceName currentAssemblyResourceName = (AssemblyResourceName)value;
          using (ResourceSelectorUI form = new ResourceSelectorUI(currentAssemblyResourceName))
          {
            if (editorService.ShowDialog(form) == DialogResult.OK)
            {
              if (form.SelectedResource != null)
              {
                currentAssemblyResourceName = form.SelectedResource;
              }
            }
          }
          return currentAssemblyResourceName;
        }
      }
      return value;
    }

		/// <summary>
		/// Gets the editor style used by the <seealso cref="UITypeEditor.GetEditStyle()"/> method.
		/// </summary>
		/// <param name="context">An <see cref="ITypeDescriptorContext"/> that can be used to gain additional context information</param>
		/// <returns>
		/// 	<see cref="UITypeEditorEditStyle.Modal"/> for this editor.
		/// </returns>
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
		#endregion
  }
}